package edu.school.controller;

import com.github.pagehelper.PageInfo;
import edu.school.entity.BigData;
import edu.school.entity.User;
import edu.school.service.UserService;
import edu.school.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Controller
public class UserController {
    @Autowired
    UserService service;
    @RequestMapping(value = "/user/add",method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String add(User record){
        Date date = new Date();
        // 得到一个Timestamp格式的时间，存入mysql中的时间格式“yyyy/MM/dd HH:mm:ss”
        Timestamp timeStamp = new Timestamp(date.getTime());
        record.setCreate_time(timeStamp);
        if(service.registe(record)){
            return "<script>alert('注册成功');location.href='/toLogin.action';</script>";
            /*  return "<script>alert('注册成功');location.href='/toLogin.action';</script>";*/
        }else{
            return "<script>alert('注册失败');history.go(-1);</script>";
        }

    }
    @RequestMapping(value = "/user/list",method = {RequestMethod.GET,RequestMethod.POST})
    public String pageList(User record,
                           @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                           @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize,
                           ModelMap modelMap){
        PageInfo<User> pageInfo = service.getPageList(pageNum,pageSize,record);
        modelMap.addAttribute("pageInfo",pageInfo);
        modelMap.addAttribute("record",record);
        return "/user/list";
    }
    @RequestMapping(value = "/user/query")
    public String query( Integer id, Model model){
        User record=service.findById(id);
        if(null!=record){
            model.addAttribute("record",record);
        }
        return "user/edit";
    }
    //用户修改功能
    @RequestMapping(value = "/user/edit",method = {RequestMethod.GET,RequestMethod.POST},produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String edit(User record,HttpSession session){
        if(service.update(record)){
            User user= (User) session.getAttribute("user");
            if (user!=null){
                return "<script>alert('修改成功');location.href='/user/myinfo';</script>";
            }
            return "<script>alert('后端修改成功');location.href='/user/list';</script>";

        }else{
            return "<script>alert('修改失败');history.go(-1);</script>";
        }

    }
    //我的个人信息查询
    @RequestMapping(value = "/user/myinfo")
    public String query( Model model, HttpSession session){
        User user= (User) session.getAttribute("user");
        if(user!=null){
            User  record=service.findById(user.getId());
            model.addAttribute("record",record);
            model.addAttribute("url","myinfo");
        }
        return "client/main";
    }
    //删除
//    @RequestMapping("/user/delete")
//    public String delete(Integer id,String filename) {
//        boolean b =service.deleteUser(id);
//        System.out.println(filename+"文件名称");
//        WebUtils.deleteImg(filename);
//        return "redirect:/user/list";
//
//
//    }
    @RequestMapping("/user/delete")
    @ResponseBody
    public String ajaxdelete(Integer id,String filename) {
        boolean b =service.deleteUser(id);
        System.out.println(filename+"文件名称");
        WebUtils.deleteImg(filename);
        if(b){
            return "ok";
        }
        return "fail";

    }
    //批量删除
    @RequestMapping("/user/deletemany")
    @ResponseBody
    public String  deletemany(String ids) {
        System.err.println(ids);
        String[] arrayids=ids.split(",");//把数组里的值逗号隔开
        System.out.println("批量删除成功");
        for (int i=0;i<arrayids.length;i++){
            User user=service.findById(Integer.parseInt(arrayids[i]));
            WebUtils.deleteImg(user.getPhoto());
        }
        service.deleteMany(arrayids);
        return "ok";//返回给ajax



    }
   //统计
   @RequestMapping(value = "/user/data")
   @ResponseBody
   public List<BigData> getBigDataList(){
       List<BigData> list = service.selectBigData();
       return list;
   }

}
